import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
    }return t;
};
import * as React from 'react';
import classNames from 'classnames';
import RcInputNumber from '../rc-components/input-number';
export function formatNumber(value) {
    var vs = String(value).split('.');
    var v = vs[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    return vs.length > 1 ? v + '.' + vs[1] : v;
}
export function parseNumber(value) {
    if (value.indexOf(",") != -1) {
        return value.replace(/,/g, "");
    }
    return value;
}

var InputNumber = function (_React$Component) {
    _inherits(InputNumber, _React$Component);

    function InputNumber() {
        _classCallCheck(this, InputNumber);

        var _this = _possibleConstructorReturn(this, (InputNumber.__proto__ || Object.getPrototypeOf(InputNumber)).apply(this, arguments));

        _this.numberFormatter = function (value) {
            var formatter = _this.props.formatter;

            var v = formatNumber(value);
            return formatter ? formatter(v) : v;
        };
        _this.numberParser = function (value) {
            var parser = _this.props.parser;

            var v = parseNumber(value);
            return parser ? parser(v) : v;
        };
        return _this;
    }

    _createClass(InputNumber, [{
        key: 'render',
        value: function render() {
            var _classNames,
                _this2 = this;

            var _a = this.props,
                form = _a.form,
                _a$allowThousandth = _a.allowThousandth,
                allowThousandth = _a$allowThousandth === undefined ? false : _a$allowThousandth,
                className = _a.className,
                size = _a.size,
                others = __rest(_a, ["form", "allowThousandth", "className", "size"]);
            var inputNumberClass = classNames((_classNames = {}, _defineProperty(_classNames, this.props.prefixCls + '-lg', size === 'large'), _defineProperty(_classNames, this.props.prefixCls + '-sm', size === 'small'), _classNames), className);
            var otherProp = others;
            if (allowThousandth) {
                otherProp = _extends({}, otherProp, { formatter: this.numberFormatter, parser: this.numberParser });
            }
            return React.createElement(RcInputNumber, _extends({ ref: function ref(c) {
                    return _this2.inputNumberRef = c;
                }, className: inputNumberClass }, otherProp));
        }
    }, {
        key: 'focus',
        value: function focus() {
            this.inputNumberRef.focus();
        }
    }, {
        key: 'blur',
        value: function blur() {
            this.inputNumberRef.blur();
        }
    }]);

    return InputNumber;
}(React.Component);

export default InputNumber;

InputNumber.defaultProps = {
    prefixCls: 'ant-input-number',
    step: 1
};